package com.jzxiang.pickerview.adapters;

import com.chinasoft_ohos.commontools.util.TextUtils;
import com.jzxiang.pickerview.config.PickerConfig;

/**
 * NumericWheelAdapter
 *
 * @since 2021-07-20
 */
public class NumericWheelAdapter implements WheelViewAdapter {

    private int minValue;
    private int maxValue;
    private String mFormat;
    private String mUnit;
    private PickerConfig mPickerConfig;

    /**
     * NumericWheelAdapter
     *
     * @param minValue minValue
     * @param maxValue maxValue
     * @param format format
     * @param unit unit
     */
    public NumericWheelAdapter(int minValue, int maxValue, String format, String unit) {
        this.mUnit = unit;
        this.mFormat = format;
        this.minValue = minValue;
        this.maxValue = maxValue;
    }

    @Override
    public PickerConfig getConfig() {
        if (mPickerConfig == null) {
            return new PickerConfig();
        }
        return mPickerConfig;
    }

    @Override
    public String getItem(int index) {
        if (index >= 0 && index < getItemCount()) {
            int value = minValue + index;
            String text = !TextUtils.isEmpty(mFormat) ? String.format(mFormat, value) : Integer.toString(value);
            text = TextUtils.isEmpty(mUnit) ? text : text + mUnit;

            return text;
        }
        return null;
    }

    @Override
    public int getItemCount() {
        return maxValue - minValue + 1;
    }

    @Override
    public void setConfig(PickerConfig pickerConfig) {
        mPickerConfig = pickerConfig;
    }
}
